package com.wptk.filemanager.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.util.Log;

import androidx.core.content.ContextCompat;


import com.wptk.filemanager.R;

import java.util.ArrayList;
import java.util.List;

public class AppUtils {


    Context mContext;

    public AppUtils(Context context) {
        mContext = context;
    }

    public List<String> getAllInstalledApkInfo() {
        List<String> apkPackageName = new ArrayList<>();
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        List<ResolveInfo> resolveInfoList = mContext.getPackageManager().queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : resolveInfoList) {
            ActivityInfo activityInfo = resolveInfo.activityInfo;
            if (!isSystemPackage(resolveInfo)) {
                apkPackageName.add(activityInfo.applicationInfo.packageName);
            }
        }
        return apkPackageName;
    }

    public List<String> getThirdAppList() {

        String selfPackageName = mContext.getPackageName();
        PackageManager packageManager = mContext.getPackageManager();
        List<PackageInfo> packageInfoList = packageManager .getInstalledPackages(0);
        List<String> thirdAPP = new ArrayList<>();
        for (int i = 0; i < packageInfoList.size(); i++) {
            PackageInfo pak = (PackageInfo)packageInfoList.get(i);
            //判断是否为系统预装的应用
            if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
                // 第三方应用
                if(!pak.packageName.equals(selfPackageName)){
                    thirdAPP.add( pak.packageName);
                }
                //Log.e("app", "第三方应用----------："+ pak.packageName);
            } else{
                //系统应用
                //Log.e("app", "系统应用："+ pak.packageName);
            }
        }
        return thirdAPP ;
    }

    //读取系统所有包名
    public List<String> allPackage() {
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<>();;
        //获取PackageManager
        PackageManager packageManager = mContext.getPackageManager();
        String selfPackageName = mContext.getPackageName();
        //LogUtils.e("selfPackageName -- " + selfPackageName);

        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);

        int thredAppNum =0;
        int systemAppNum =0;
        //从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null && packageInfos.size()>0) {
            for (int i = 0; i < packageInfos.size(); i++) {

                String packName = packageInfos.get(i).packageName;
                if (isSystemPackage(packageInfos.get(i))
                        || packName.contains("android")){

                    //系统应用
                    //LogUtils.e( "系统应用------"+packName);
                    systemAppNum++;
                }else{
                    //还需要加上排除自己厂家的app
                    if(!packName.equals(selfPackageName)){
                        String appName = getAppName(packName);
                        if(appName !=null && !appName.startsWith("com.")){
                            packageNames.add(packName);
                            //LogUtils.e( "第三方应用---------------"+ packName);
                            thredAppNum++;
                        }
                    }
                }
            }
        }

        //LogUtils.e("thredAppNum  = " + thredAppNum);
        //LogUtils.e( "systemAppNum  = " + systemAppNum);
        return packageNames;

    }

    public boolean isSystemPackage(PackageInfo packageInfo){

        return ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0);
    }


    public boolean isSystemPackage(ResolveInfo resolveInfo) {
        return ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0);
    }

    public Drawable getAppIconByPackageName(String ApkTempPackageName) {
        Drawable drawable;
        try {
            drawable = mContext.getPackageManager().getApplicationIcon(ApkTempPackageName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            drawable = ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher);
        }
        return drawable;
    }

    public String getAppName(String ApkPackageName) {
        String Name = "";
        ApplicationInfo applicationInfo;
        PackageManager packageManager = mContext.getPackageManager();
        try {
            applicationInfo = packageManager.getApplicationInfo(ApkPackageName, 0);
            if (applicationInfo != null) {
                Name = (String) packageManager.getApplicationLabel(applicationInfo);
            }
        } catch (PackageManager.NameNotFoundException e) {

            e.printStackTrace();
        }
        return Name;
    }
    /**
     * 获取安装包icon
     * @param context
     * @param apkPath
     * @return
     */
    public static Drawable getApkIcon(Context context, String apkPath) {
        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(apkPath,
                PackageManager.GET_ACTIVITIES);
        if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;
            appInfo.sourceDir = apkPath;
            appInfo.publicSourceDir = apkPath;
            try {
                return appInfo.loadIcon(pm);
            } catch (OutOfMemoryError e) {
                Log.e("ApkIconLoader", e.toString());
            }
        }
        return null;
    }

}
